﻿using System;
using System.Xml;

namespace DBConnection
{
    public static class MyDebugClass
    {
        /*public static void LogThis(Exception exc)
        {
            String msg = "";
            msg += DateTime.Now.ToLongTimeString();
            msg += ": " + exc.Message;
            msg += Environment.NewLine + "+   Help link: " + exc.HelpLink;
            msg += Environment.NewLine + "+   Source: " + exc.Source;
            msg += Environment.NewLine + "+   Stack trace: " + exc.StackTrace;
            msg += Environment.NewLine + "+   Target Site: " + exc.TargetSite;
            if (exc.InnerException != null)
            {
                msg += Environment.NewLine + "+   Inner exception: " + exc.InnerException.Message;
                msg += Environment.NewLine + "     Help link: " + exc.InnerException.HelpLink;
                msg += Environment.NewLine + "     Source: " + exc.InnerException.Source;
                msg += Environment.NewLine + "     Stack trace: " + exc.InnerException.StackTrace;
                msg += Environment.NewLine + "     Target Site: " + exc.InnerException.TargetSite;
            }
            msg += Environment.NewLine + "***********************************" + Environment.NewLine;
            FileUtility.WriteLine("debug.txt", msg);
        }*/

        public static string ReplaceSpecialChar(string origin)
        {
            if (origin == null || origin == "")
                return "";

            origin = origin.Replace("&", "&amp;");
            origin = origin.Replace("<", "&lt;");
            origin = origin.Replace(">", "&gt;");
            origin = origin.Replace("\"", "&quot;");
            origin = origin.Replace("'", "&apos;");
            return origin;
        }

        public static void LogThis(string action, Exception exc)
        {
            string bugFile = "bugs\\bugs_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xml";
            string count = FileUtility.GetAttributeXml(bugFile, "exceptions", "count");
            if (count == null)
                count = "0";
            string msg = "";
            msg += "<exception>";
            msg += "<id>" + (Int32.Parse(count) + 1).ToString() + "</id>";
            msg += "<date>" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "</date>";
            msg += "<action>" + ReplaceSpecialChar(action) + "</action>";
            msg += "<message>" + exc.Message + "</message>";
            msg += "<type>" + exc.GetType().ToString() + "</type>";
            msg += "<helplink>" + ReplaceSpecialChar(exc.HelpLink) + "</helplink>"; // most of the time, HelpLink is null:(
            msg += "<source>" + ReplaceSpecialChar(exc.Source) + "</source>";
            msg += "<stacktrace>" + ReplaceSpecialChar(exc.StackTrace) + "</stacktrace>";
            msg += "<targetsite>" + exc.TargetSite + "</targetsite>";
            if (exc.InnerException != null)
            {
                msg += "<innerexception>";
                msg += "<innerexceptionmessage>" + exc.InnerException.Message + "</innerexceptionmessage>";
                msg += "<innerexceptiontype>" + exc.InnerException.GetType().ToString() + "</innerexceptiontype>";
                msg += "<innerexceptionhelplink>" + ReplaceSpecialChar(exc.InnerException.HelpLink) + "</innerexceptionhelplink>";
                msg += "<innerexceptionsource>" + ReplaceSpecialChar(exc.InnerException.Source) + "</innerexceptionsource>";
                msg += "<innerexceptionstacktrace>" + ReplaceSpecialChar(exc.InnerException.StackTrace) + "</innerexceptionstacktrace>";
                msg += "<innerexceptiontargetsite>" + exc.InnerException.TargetSite + "</innerexceptiontargetsite>";
                msg += "</innerexception>";
            }
            msg += "</exception>";

            try
            {
                string[] attNames = { "count" };
                string[] attValues = { "0" };
                FileUtility.WriteXml(bugFile, "exceptions", attNames, attValues, msg);
                FileUtility.SetAttributeXml(bugFile, "exceptions", "count", (Int32.Parse(count) + 1).ToString());
            }
            catch
            {
                return;
            }
        }
    }
}
